package live.jialing.sip.base.rest;

import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import live.jialing.core.beanvalidator.First;
import live.jialing.core.web.controller.SimpleBaseRestController;
import live.jialing.sip.base.domain.Parent;
import live.jialing.sip.base.service.ParentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 家长接口
 */
@Api(value = "ParentRestController", description = "家长接口")
@RestController
@RequestMapping(value = "/parents")
public class ParentRestController extends SimpleBaseRestController<Parent, Long> {

    private static Logger logger = LoggerFactory.getLogger(ParentRestController.class);

    @Resource
    private ParentService parentService;

    @Override
    public ParentService getService() {
        return this.parentService;
    }

    /**
     * 新增家长并关联学生
     *
     * @param childId 孩子ID
     * @param parent  家长数据
     */
    @ApiOperation(value = "新增家长并关联学生")
    @ApiParam(name="childId",value = "孩子ID")
    @RequestMapping(value = "/child", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void createAndRelationChild(Long childId, @ApiParam @Validated({First.class}) Parent parent) {

        org.apache.commons.lang3.Validate.notNull(childId, "孩子ID为空！");
        parentService.addAndRelationChild(childId, parent);
    }

    /**
     * 删除家长与学生的关系
     *
     * @param childId  孩子ID
     * @param parentId 家长ID
     */
    @ApiOperation(value = "删除家长与学生的关系")
    @RequestMapping(value = "/child", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void unRelationChild(@ApiParam Long childId, @ApiParam Long parentId) {

        org.apache.commons.lang3.Validate.notNull(childId, "孩子ID为空！");
        org.apache.commons.lang3.Validate.notNull(parentId, "家长ID为空！");
        parentService.unRelationChild(childId, parentId);
    }

    /**
     * 学生的所有家长信息
     *
     * @param childId 孩子ID
     * @return
     */
    @RequestMapping(value = "/child", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<Parent> findParentByChild(Long childId) {

        org.apache.commons.lang3.Validate.notNull(childId, "孩子ID为空！");
        Map<String, Object> searchParams = Maps.newTreeMap();
        searchParams.put("EQ_children.id", childId);
        return parentService.findAll(searchParams, null);
    }


}
